package picounit.java.collection;

import java.util.Iterator;

import picounit.java.Factory;

public class Iterable<T> implements java.lang.Iterable<T> {
    public Iterable(Iterator<T> iterator) {
        this(new IterableFromIterator<T>(iterator));
    }

    public Iterable(java.lang.Iterable<T> iterable) {
        this.iterable = iterable;
    }

    public boolean passes(Filter<T> filter) {
        return filter(filter).iterator().hasNext();
    }

    public Iterable<T> filter(final Filter<T> filter) {
        return new Iterable<T>(new FilterIterator<T>(iterator(), filter)) {
            @Override
            public Iterator<T> iterator() {
                return new FilterIterator<T>(Iterable.this.iterator(), filter);
            }
        };
    }

    public <To> Iterable<To> convert(final Factory<To, T> factory) {
        return new Iterable<To>(new ConvertingIterator<To, T>(iterator(), factory)) {
            @Override
            public Iterator<To> iterator() {
                return new ConvertingIterator<To, T>(Iterable.this.iterator(), factory);
            }
        };
    }

    public Iterator<T> iterator() {
        return iterable.iterator();
    }

    private final java.lang.Iterable<T> iterable;
}
